package cn.sunzq.springdata.elasticsearch.demo.querydemo.controller;

import cn.sunzq.springdata.elasticsearch.demo.base.response.ResponseDTO;
import cn.sunzq.springdata.elasticsearch.demo.document.TempLincolnshireDocument;
import cn.sunzq.springdata.elasticsearch.demo.querydemo.service.TempLincolnshireDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @创建人 孙中泉
 * @创建时间 2021/7/27
 * @描述
 */
@RestController
@RequestMapping("/tempLincolnshire")
public class TempLincolnshireController {

    @Autowired
    private TempLincolnshireDemoService tempLincolnshireDemoService;

    @RequestMapping(value = {"/criteriaQuery1/{name}/{year}/{pageNo}/{pageSize}", "/criteriaQuery1/{name}/{year}/{pageNo}", "/criteriaQuery1/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> criteriaQuery1(@PathVariable("name") String name,
                                                                           @PathVariable("year") String year,
                                                                      @PathVariable(value = "pageNo",required = false) Integer pageNo,
                                                                      @PathVariable(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.criteriaQuery1(name, year, pageNo, pageSize));
    }

    @RequestMapping(value = "/criteriaQuery2", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> criteriaQuery2(@RequestParam(value = "name", required = false) List<String> name,
                                                                           @RequestParam(value = "year", required = false) List<String> year,
                                                                           @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                                           @RequestParam(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.criteriaQuery2(name, year, pageNo, pageSize));
    }

    @RequestMapping(value = "/criteriaQuery3", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> criteriaQuery3(@RequestParam(value = "keyword",required = false) String keyword,
                                                                           @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                                           @RequestParam(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.criteriaQuery3(keyword, pageNo, pageSize));
    }

    @RequestMapping(value = {"/stringQuery1/{name}/{year}/{pageNo}/{pageSize}", "/stringQuery1/{name}/{year}/{pageNo}", "/stringQuery1/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> stringQuery1(@PathVariable("name") String name,
                                                                    @PathVariable("year") String year,
                                                                    @PathVariable(value = "pageNo",required = false) Integer pageNo,
                                                                    @PathVariable(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.stringQuery1(name, year, pageNo, pageSize));
    }

    @RequestMapping(value = {"/restClient1/{name}/{year}/{pageNo}/{pageSize}", "/restClient1/{name}/{year}/{pageNo}", "/restClient1/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String,Object>>> restClient1(@PathVariable("name") String name,
                                                             @PathVariable("year") String year,
                                                             @PathVariable(value = "pageNo",required = false) Integer pageNo,
                                                             @PathVariable(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.restClient1(name, year, pageNo, pageSize));
    }

    @RequestMapping(value = {"/aggDemo1/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<Map<String,Object>> aggDemo1(@PathVariable("name") String name,
                                                     @PathVariable("year") String year){
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo1(name, year));
    }

    @RequestMapping(value = {"/aggDemo2/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String, Object>>> aggDemo2(@PathVariable("name") String name,
                                                    @PathVariable("year") String year){
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo2(name, year));
    }


    @RequestMapping(value = {"/aggDemo3/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String, Object>>> aggDemo3(@PathVariable("name") String name,
                                                    @PathVariable("year") String year){
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo3(name, year));
    }

    @RequestMapping(value = {"/aggDemo4/{name}/{year}"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String,Object>>> aggDemo4(@PathVariable("name") String name,
                                                    @PathVariable("year") String year,
                                                    @RequestParam("valueGroup") List<Double> valueGroup){
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo4(name, year, valueGroup));
    }

    @RequestMapping(value = {"/aggDemo5"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String,Object>>> aggDemo5(String  dateRangeStart, String dateRangeEnd){
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo5(dateRangeStart, dateRangeEnd));
    }

    @RequestMapping(value = {"/aggDemo6"}, method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<Map<String,Object>>> aggDemo6(String  dateRangeStart, String dateRangeEnd, int topNo, boolean isAsc, String aggField){
        if(topNo <= 0) topNo = 10;
        return ResponseDTO.defaultResponse(tempLincolnshireDemoService.aggDemo6(dateRangeStart, dateRangeEnd, topNo, isAsc, aggField));
    }
}
